#include "edxrdtrans.h"
#include "frame.h" 
#include "../logic.h" 
#include "string.h"
#include <wx/radiobox.h>

using namespace std;


enum
{
    ID_EDXRD_TRANSMODE = 101,
    ID_EDXRD_USE_REMPARA ,
	ID_EDXRD_BTN1 ,
	ID_EDXRD_BTN2 ,
};
/*
BEGIN_EVENT_TABLE(EDXRDtrans, wxFrame)
    EVT_MENU(ID_EDXRD_BTN2, EDXRDtrans::OnQuit)
END_EVENT_TABLE()
*/

EDXRDtrans::EDXRDtrans(const wxString& title)
       : wxDialog(NULL, -1, title, wxPoint(-1, -1), wxSize(450, 270))
{

  wxPanel *panel = new wxPanel(this, -1);

  wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);  // base of the sizer

  wxBoxSizer *upperbox = new wxBoxSizer(wxVERTICAL); // calibrationi parameters
 	 wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL);	// formula 1
  		wxStaticText *st1_1 =  new wxStaticText(panel, wxID_ANY, wxT("E ="));
		tc1_1 = new wxTextCtrl(panel, wxID_ANY);
		wxStaticText *st1_2 =  new wxStaticText(panel, wxID_ANY, wxT(" + "));	
		tc1_2 = new wxTextCtrl(panel, wxID_ANY);
		wxStaticText *st1_3 =  new wxStaticText(panel, wxID_ANY, wxT("* #CH "));

		hbox1->Add(st1_1, 0);
		hbox1->Add(tc1_1, 1);
		hbox1->Add(st1_2, 0);
		hbox1->Add(tc1_2, 1);
		hbox1->Add(st1_3, 1);

	wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);	// formula 2
  		wxStaticText *st2_1 =  new wxStaticText(panel, wxID_ANY, wxT("theta ="));
		tc2_1 = new wxTextCtrl(panel, wxID_ANY);
		wxStaticText *st2_2 =  new wxStaticText(panel, wxID_ANY, wxT("degrees"));

		hbox2->Add(st2_1, 0);
		hbox2->Add(tc2_1, 1);
		hbox2->Add(st2_2, 0);

	upperbox->Add(hbox1, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10);
	upperbox->Add(hbox2, 0, wxLEFT | wxRIGHT | wxTOP, 10);

  vbox->Add(upperbox, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10);

  	wxBoxSizer *lowerbox = new wxBoxSizer(wxHORIZONTAL); // everything else
		wxArrayString choice;
		choice.Add("Channel Number (#ch)");
		choice.Add("Energy (keV)");
		choice.Add("d-spacing (Angstrom)");
		choice.Add("1/d-spacing (Angstrom^-1)");
		RB = new wxRadioBox (panel, ID_EDXRD_TRANSMODE, wxT("Desire Unit"), wxDefaultPosition, wxDefaultSize,  choice, 1, wxRA_SPECIFY_COLS);

	lowerbox->Add(RB, 0, wxLEFT, 10);  

	wxBoxSizer *lower_r_box = new wxBoxSizer(wxVERTICAL);		
	  	cb = new wxCheckBox(panel, ID_EDXRD_USE_REMPARA, wxT("Use Parameters in RAM?"));
		Connect( ID_EDXRD_USE_REMPARA, wxEVT_COMMAND_CHECKBOX_CLICKED ,(wxObjectEventFunction) &EDXRDtrans::OnCBChanged );

		wxBoxSizer *button_hbox = new wxBoxSizer(wxHORIZONTAL);
			wxButton *btn1 = new wxButton(panel, ID_EDXRD_BTN1, wxT("Ok"));
			Connect( ID_EDXRD_BTN2, wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction) &EDXRDtrans::OnQuit );
  			button_hbox->Add(btn1, 0);
  			wxButton *btn2 = new wxButton(panel, ID_EDXRD_BTN2, wxT("Close"));
  			button_hbox->Add(btn2, 0, wxLEFT | wxBOTTOM , 5);
			Connect( ID_EDXRD_BTN1, wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction) &EDXRDtrans::OnETrans );

		lower_r_box->Add(cb,0);
		lower_r_box->Add(button_hbox,0);
	lowerbox->Add(lower_r_box, 0, wxLEFT, 20);  
 vbox->Add(lowerbox, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10);
			

 panel->SetSizer(vbox);

 Centre();
}


void EDXRDtrans::OnQuit(wxCommandEvent&)
{
    Close(true);
}

void EDXRDtrans::OnETrans(wxCommandEvent&)
{
    //ftk->exec("hello");


	if(RB->GetSelection()==1&& wx2s(tc1_1->GetValue()).length()>0 && wx2s(tc1_2->GetValue()).length()>0)
	{
		EDXRDtrans::recoveroldtrans();
		ftk->exec("@*: X= "+ wx2s(tc1_1->GetValue()) +"+ "+wx2s(tc1_2->GetValue())+"*x");
		EDXRDtrans::updateparameters();
	}


	else if(RB->GetSelection()==2 && wx2s(tc1_1->GetValue()).length()>0 && wx2s(tc1_2->GetValue()).length()>0 && wx2s(tc2_1->GetValue()).length()>0)
	{
		EDXRDtrans::recoveroldtrans();
		ftk->exec("@*: X= "+wx2s(tc2_1->GetValue())+"/("+ wx2s(tc1_1->GetValue()) +"+ "+wx2s(tc1_2->GetValue())+"*x)");
		EDXRDtrans::updateparameters();
	}


	else if(RB->GetSelection()==3 && wx2s(tc1_1->GetValue()).length()>0 && wx2s(tc1_2->GetValue()).length()>0 && wx2s(tc2_1->GetValue()).length()>0)
	{
		EDXRDtrans::recoveroldtrans();
		ftk->exec("@*: X= ("+ wx2s(tc1_1->GetValue()) +"+ "+wx2s(tc1_2->GetValue())+"*x)/"+ wx2s(tc2_1->GetValue()) );
		EDXRDtrans::updateparameters();
	}
}

void EDXRDtrans::OnCBChanged(wxCommandEvent&)
{

	if(cb->GetValue()==0)
	{
		RB->SetSelection(0);
		tc1_1->SetValue("");
		tc1_2->SetValue("");
		tc2_1->SetValue("");
	}
	if(cb->GetValue()==1)
	{
		RB->SetSelection(ftk->old_model);
		if(ftk->old_a.length()>0) tc1_1->SetValue(ftk->old_a);
		if(ftk->old_b.length()>0) tc1_2->SetValue(ftk->old_b);
		if(ftk->old_c.length()>0) tc2_1->SetValue(ftk->old_c);
	}

}


void EDXRDtrans::updateparameters()
{

	if(RB->GetSelection()>0){ 
		ftk->old_model=  RB->GetSelection();

		if (wx2s(tc1_1->GetValue()).length()>0)
		{ ftk->old_a=wx2s(tc1_1->GetValue()); } 

		if (wx2s(tc1_2->GetValue()).length()>0) 
		{ ftk->old_b=wx2s(tc1_2->GetValue()); }

		if (wx2s(tc2_1->GetValue()).length()>0)
		{ ftk->old_c=wx2s(tc2_1->GetValue()); }
	}

}

void EDXRDtrans::recoveroldtrans()
{
	if(ftk->old_model==1)
	{
		ftk->exec("@*: X= ( x -"+ ftk->old_a + ") /+ "+ ftk->old_b);
	}
	if(ftk->old_model==2)
	{
		ftk->exec("@*: X= (" + ftk->old_c + "/x -" + ftk->old_a + ")/ "+ ftk->old_b );
	}
	if(ftk->old_model==3)
	{
		ftk->exec("@*: X= ( x *"+ ftk->old_c+" -"+ ftk->old_a + ") /+ "+ ftk->old_b);
	}

}

